//
//  YMToolChooseStatusSheet.m
//  YMMedical
//
//  Created by y s on 2023/7/25.
//

#import "YMToolChooseStatusSheet.h"

@interface YMToolChooseStatusSheet ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *vPicker;

@property (nonatomic, assign) NSInteger indexNow;

@end

@implementation YMToolChooseStatusSheet

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{

    [self.vBg addSubview:self.vPicker];
    [self.vPicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-IPHONE_X_BOTTOM_HEIGHT);
    }];
    WEAK_SELF;
    self.sureClickBlock = ^{
        if (weakSelf.textSureBlock) {
            weakSelf.textSureBlock(weakSelf.arrData[weakSelf.indexNow], weakSelf.indexNow);
        }
    };
}

- (void)setArrData:(NSArray *)arrData{
    _arrData = arrData;
    [self.vPicker reloadAllComponents];
    if (_nowSelect != nil && _nowSelect.length > 0){
        if ([arrData containsObject:_nowSelect]){
            NSInteger indexNow = [arrData indexOfObject:_nowSelect];
            [self.vPicker selectRow:indexNow inComponent:0 animated:NO];
        }
    }
}

#pragma mark -- UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.arrData.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *strText = @"";
    if (row < self.arrData.count) {
        strText = [SDataUtil stringOf:self.arrData[row]];
    }
    return strText;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return SCREEN_WIDTH*0.5;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return [SFitUtil fit:42];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (row < self.arrData.count) {
        self.indexNow = row;
    }
}

#pragma mark -- setter and getter
- (UIPickerView *)vPicker{
    if (_vPicker == nil) {
        _vPicker = [[UIPickerView alloc] init];
        _vPicker.delegate = self;
        _vPicker.dataSource = self;
    }
    return _vPicker;
}

@end
